//
//  CJForwardRevenusController.m
//  Boss
//
//  Created by admin on 2018/9/19.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "CJForwardRevenusController.h"
#import "CJForwardRevenusCell.h"
#import "CJForwardRevenusModel.h"
@interface CJForwardRevenusController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic)NSUInteger page;
@end

@implementation CJForwardRevenusController
-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
   
    
    self.page = 1;
    [self loadData];
    WEAKSELF
    self.tableView.mj_footer=[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
       // weakSelf.refresh=NO;
        weakSelf.page++;
        // [weakSelf loadTableData];
        [weakSelf loadData];
    }];
    
}

-(void)loadData
{
    WEAKSELF
    NSString *page = [NSString stringWithFormat:@"%ld",self.page];
    [WYHttpServer getTransferEarningsListWithCurrentPage:page WithPageSize:@"20" SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
       
        if (success) {
            [weakSelf.dataArray addObjectsFromArray:[CJForwardRevenusModel getForwardRevenusModelFromJson:response[@"result"][@"detail"]]];
            [weakSelf.tableView reloadData];
            if ([response[@"result"][@"hasNextPage"] integerValue]) {
                [weakSelf.tableView.mj_footer endRefreshing];
                
                
                
            }else{
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }else{
            [weakSelf toast:errorMessage];
        }
        
        
        
    }];
}

-(void)setupUI
{
    self.navigationItem.title = @"转发收益";
    [self.view addSubview:self.tableView];
    self.tableView.estimatedRowHeight = 74;
    [self.tableView registerNib:[UINib nibWithNibName:@"CJForwardRevenusCell" bundle:nil] forCellReuseIdentifier:@"CJForwardRevenusCell"];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CJForwardRevenusCell *cell =[tableView dequeueReusableCellWithIdentifier:@"CJForwardRevenusCell" forIndexPath:indexPath];
    if (!cell) {
        cell =[[NSBundle mainBundle]loadNibNamed:@"CJForwardRevenusCell" owner:nil options:nil].firstObject;
    }
    CJForwardRevenusModel *model = self.dataArray[indexPath.row];
    [cell setModel:model];
    
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    return cell;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UITableView *)tableView
{
    if (_tableView == nil) {
        CGRect frame = CGRectMake(0, self.navHeight, kScreenWidth, kScreenHeight-self.navHeight-self.tabHeight);
        _tableView = [[UITableView alloc] initWithFrame:frame
                                                  style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
    }
    return _tableView;
}

@end
